<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/30
 * Time: 13:20
 */

namespace Admin\Controller;
use Think\Controller;
class RoomorderController extends Controller
{
    public function index()
    {
        $this->checkSession();
        $this->display();
    }
    public function  start_stay(){//办理入住
        $this->checkSession();
        $user_name = $_POST['user_name'];//提取用户名
        $user_idcard = $_POST['user_idcard']; // 提取密码并进行加密
        $now = date('Y-m-d');
        $yesterday=date("Y-m-d",strtotime("-1 day"));

        //账号匹配
        /*$record = M('room_order');
        if ($order =$record->where("user_name='$user_name' AND user_IDcard='$user_idcard'AND starttime='$now'and instatus='0' and order_status='1'")->count()) {
            $per = 5;
            import('Org.Util.Page');
            $page = new \Page($order,$per);*/



        $room_order=M('room_order');
        $check_roomid=$room_order->where("user_name='$user_name' and starttime='$now' and instatus='待入住'and order_status='到店付'")->field('room_id')->select();//获取入住用户的房间号
        $c_r=$check_roomid[0]{'room_id'};//获取入住用户的房间号


        $check_roomid1=$room_order->where("user_name='$user_name' and starttime='$now' and instatus='待入住'and order_status='已支付'")->field('room_id')->select();//获取入住用户的房间号
        $c_r1=$check_roomid1[0]{'room_id'};//获取入住用户的房间号


        $old_user=$room_order->where("room_id='$c_r' and endtime='$now' and instatus='已入住' and order_status='已支付'")->field('room_id')->select();//利用所获取的房间号再去找前一天入住的用户 到店付
        $o_u=$old_user[0]{'room_id'};
      ;

        $old_user1=$room_order->where("room_id='$c_r1' and endtime='$now' and instatus='已入住' and order_status='已支付'")->field('room_id')->select();//利用所获取的房间号再去找前一天入住的用户 已支付
        $o_u1=$old_user1[0]{'room_id'};


        // $check_roomid = "select * from hotel_room_order where user_IDcard='$user_idcard'AND endtime='$now'and instatus='0' and order_status='1'";//显示订单退房时间在今天
        if(empty($o_u)&&empty($o_u1)){//昨天入住订单未办理退房
            $sql = "select * from hotel_room_order where user_IDcard='$user_idcard'AND starttime>='$yesterday'and instatus='待入住' and (order_status='到店付' or order_status='已支付') ";//显示订单入住时间在今天

            if(empty($sql)==false) {
                $query = M('room_order')->query($sql);
                $this->assign('param', $query);
                $this->display();
            }else{
                $this->error('该客户无入住今日订单！');
            }
        }else{
            $this->error("该房间的客人未办理退房，请先办理退房再进行该操作！");
        }
        //}

    }
    public function endstay(){
        $this->checkSession();
        $this->display();
    }
    public function end_stay(){//退房
        $this->checkSession();
        $room_id = $_POST['room_id'];//提取用户名
        $user_name = $_POST['user_name']; //
        //$this->edit_out($room_id);
        $yesterday=date("Y-m-d",strtotime("-1 day"));
        $tomorrow=date("Y-m-d",strtotime("+1 day"));

        //账号匹配
        /* $record = M('room_order');
         if ($order =$record->where("room_id='$room_id' AND user_name='$user_name'and starttime='$now'")->count()) {
             $per = 5;
             import('Org.Util.Page');
             $page = new \Page($order,$per);*/
        $sql = "select * from hotel_room_order where user_name='$user_name'and endtime>'$yesterday' and room_id='$room_id'  and  instatus='已入住' and order_status='已支付'";
        if(empty($sql)==false){
            $query = M('room_order') -> query($sql);
            // $pageList=$page->fpage();
            $this->assign('param',$query);
            //  $this->assign('pageList',$pageList);
            // $this->assign('num',1);

            $this->display();
        }else{
            $this->error("暂无该用户的今日退房订单记录");
        }
        //}
    }

    public function editDisplay(){//显示结果页
        $this->checkSession();
        $r_order_id = $_GET['r_order_id'];

        $info = M('room_order')->where("r_order_id=$r_order_id")->find();
        $this->assign('info',$info);
    }
    public function edit_instatus(){//显示入住具体订单
        $this->checkSession();
        $this->editDisplay();
        $this->display();
    }
    public function  edit_in(){//真正实现入住修改状态
        $this->checkSession();
        $r_order_id=$_POST['r_order_id'];
        $order_status=M('room_order')->where("r_order_id='$r_order_id'")->field("order_status")->select();
        $room_id=M('room_order')->where("r_order_id='$r_order_id'")->field("room_id")->select();

        if($order_status[0]{"order_status"}=='取消'){
            $this->error('该订单已被取消，无法操作！');
        }else if($order_status[0]{"order_status"}=='已支付') {
            $status = $_POST['instatus_change'];
            $now = date('Y-m-d H-m-s');
            $a = '待入住';
            if ($a != $status) {
                $data['instatus'] = $_POST['instatus_change'];//
                $condition['r_order_id'] = $_POST['r_order_id'];
                $data['operator'] = $_SESSION['employe_id'];
                $data['in_time'] = $now;
                $result = M('room_order')->where($condition)->data($data)->save();
                if ($result !== false) {
                    $data['time']=$now;
                    $data['operation']="办理入住";
                    $data['operator']=$_SESSION['employe_id'];
                    $data['order_id']=$r_order_id;
                    $data['room_id']=$room_id[0]{'room_id'};
                    M('log')->add($data);
                    $this->success('已支付房费，办理入住成功！', U('roomorder/index'));
                } else {
                    $this->error('该房间还未做退房处理', U('roomorder/index'));
                }
            } else {
                $this->error('入住状态未更改！');
            }
        }else{
            $money=M('room_order')->where("r_order_id='$r_order_id'")->field("price")->select();
            $pay_money=$money[0]{'price'};//显示订单价
            $data['order_status']='已支付  ';
            $status = $_POST['instatus_change'];
            $now = date('Y-m-d H-m-s');
            $a = '待入住';
            if ($a != $status) {
                $data['instatus'] = $_POST['instatus_change'];//
                $condition['r_order_id'] = $_POST['r_order_id'];
                $data['operator'] = $_SESSION['employe_id'];
                $data['in_time'] = $now;
                $result = M('room_order')->where($condition)->data($data)->save();
                if ($result !== false) {
                    $data['time']=$now;
                    $data['operation']="办理入住(到店付订单)";
                    $data['operator']=$_SESSION['employe_id'];
                    $data['order_id']=$r_order_id;
                    $data['room_id']=$room_id[0]{'room_id'};
                    M('log')->add($data);
                    $this->success("办理入住成功！需要交纳房费总计 $pay_money 元", U('roomorder/index'),10);
                } else {
                    $this->error('该房间还未做退房处理', U('roomorder/index'),5);
                }
            } else {
                $this->error('入住状态未更改！');
            }
        }
    }

    public function edit_outstatus(){//显示退房具体订单
        $this->checkSession();
        $this->editDisplay();
        $this->display();
    }

    public function  edit_out($room_id){//真正实现退房修改状态
        $this->checkSession();
        $status=$_POST['instatus_change'];


        $r_order_id=$_POST['r_order_id'];

        //获取退房时间 - 当天时间 =剩余时间
        $a =strtotime($_POST['startDate']);
        $b=strtotime($_POST['endDate']);
        $test=$b-$a;
        $days= floor($test/(3600*24));//锁住预定时间段为1


        $room_order=M('room_order');
        $endtime=$room_order->where("r_order_id='$r_order_id'")->field('endtime')->select();
        $end=$endtime[0]{'endtime'};
        $a =strtotime($end);//退房时间

        $now=strtotime(date('Y-m-d'));//当天时间


        //获取退房时间-入住时间 =总天数
        $starttime=$room_order->where("r_order_id='$r_order_id'")->field('starttime')->select();

        $start=$starttime[0]{'starttime'};
        $b=strtotime($start);


        if($b!==$now) {

            $test =  $now-$b;
            $days = floor($test / (3600 * 24));//剩余时间
            $real_dayes = (int)$days;//剩余天数

        }else{
            $real_dayes=1;
        }


        $all=$a-$b;
        $all1= floor($all/(3600*24));//总时间
        $all_days=(int)$all1;

        //退还金额=（1-剩余时间/总天数）*总金额
        $price=$room_order->where("r_order_id='$r_order_id'")->field('price')->select();
        $price1=(int)$price[0]{'price'};
        //记录退房时间



        $out_money=($all_days-$real_dayes)*$price1/$all_days;//本应归还收益
        $fee=$out_money*0.2; //手续费

        $out_money=$out_money-$fee;//本应归还收益

        $real_money=$price1-$out_money;//实际收益

        $room_id=M('room_order')->where("r_order_id=$r_order_id")->field('room_id')->select();
        $a="已入住";

        if($a!=$status) {
            $data['out_time']=date('Y-m-d H-m-s');
            $data['real_money']=$real_money;
            $data['instatus'] = $_POST['instatus_change'];
            $condition['r_order_id'] = $_POST['r_order_id'];
            $data['operator']=$_SESSION['employe_id'];

            $result = M('room_order')->where($condition)->data($data)->save();
            if ($result !== false) {
                $data['time']=date("Y-m-d H-m-s");
                $data['operation']="办理退房";
                $data['operator']=$_SESSION['employe_id'];
                $data['order_id']=$r_order_id;
                $data['room_id']=$room_id[0]{'room_id'};
                M('log')->add($data);
                $this->success("办理退房成功！实际应收金额 $real_money 元(其中手续费20%为 $fee 元)，需退还客户现金 $out_money 元",U('roomorder/endstay'),10);
            } else {
                $this->error('已经办理退房了！',U('roomorder/endstay'),5);
            }
        }else{
            $this->error('入住状态未更改！');
        }
    }



    public function storepay(){//临时客户入住办理
        $this->checkSession();
        $this->display();
    }
    public function storepay_result()//房间搜索登记入住页
    {
        $this->checkSession();
        $starttime = $_POST['startDate'];//入住时间
        $endtime = $_POST['endDate'];//退房时间
        $a =strtotime($_POST['startDate']);
        $b=strtotime($_POST['endDate']);
        $test=$b-$a;
        $days= floor($test/(3600*24));//入住几天
        $type = $_POST['category'];
        $roomId_count = M('room')->where("room_category='$type'")->field('room_id')->count();//数组长度
        $roomId = M('room')->where("room_category='$type'")->field('room_id')->select();//数组内容
        $sum = 0;
        $n=0;
        for ($i = 0; $i <= $roomId_count; $i++) {//循环输出数组内容

            $rid = $roomId[$i]['room_id'];

            $room_order = M('room_order');
            $a = $room_order->where("room_id='$rid'and starttime<='$starttime'and endtime>'$starttime' and starttime<'$endtime' and endtime>='$endtime'and order_status='到店付'")->select();//内包含
            $a1 = $room_order->where("room_id='$rid'and starttime<='$starttime'and endtime>'$starttime' and starttime<'$endtime' and endtime>='$endtime'and order_status='已支付'")->select();//内包含

            $b = $room_order->where("room_id='$rid'and starttime<='$starttime'and endtime>'$starttime' and  endtime<='$endtime'and order_status='到店付'")->select();//右半包含
            $b1 = $room_order->where("room_id='$rid'and starttime<='$starttime'and endtime>'$starttime' and  endtime<='$endtime'and order_status='已支付'")->select();//右半包含

            $c = $room_order->where("room_id='$rid'and starttime>='$starttime'and  starttime<'$endtime' and endtime>='$endtime'and order_status='到店付'")->select();//左半包含
            $c1 = $room_order->where("room_id='$rid'and starttime>='$starttime'and  starttime<'$endtime' and endtime>='$endtime'and order_status='已支付'")->select();//左半包含

            $d = $room_order->where("room_id='$rid'and starttime>='$starttime' and endtime<='$endtime'and order_status='到店付'")->select();//全包含
            $d1 = $room_order->where("room_id='$rid'and starttime>='$starttime' and endtime<='$endtime'and order_status='已支付'")->select();//全包含

            $t = $roomId_count;//该类型的房间数量

            if ($rid != $roomId[$t]['room_id']) {
                if (empty($a) && empty($a1) && empty($b) && empty($b1) && empty($c) && empty($c1) && empty($d) && empty($d1)) {

                    $sum = $sum+1;
                    $price=M('room')->where("room_id='$rid'")->field('room_price')->select();
                    $room_price=$price[0]{'room_price'};
                    $id[$n]['room_id']=$rid;
                    $n+=1;
                } else {
                    continue;
                }
            } else {
                break;
            }
        }
        if($sum==0){
            $this->error('抱歉，该时间段该房型已经满房！',U('roomorder/storepay'),5);
        }

        $sum_price=$days*(int)$price[0]{'room_price'};//总价

        $this->assign('list',$id);
        $this->assign('category',$type);//房间类型
        $this->assign("starttime",$starttime);//入住时间
        $this->assign("endtime",$endtime);//退房时间
        $this->assign("sum_price",$sum_price);//总价
        $this->assign("days",$days);//天数
       // $this->assign('room_id',$id[0]['room_id']);//可入住的第一间房
        $this->assign('room_price',$room_price);//单价
        $this->assign('sum', $sum);//总空余房间数
        $this->display();
    }
    public function submit_result(){
        $this->checkSession();
        $category=$_POST['category'];
        $starttime=$_POST['starttime'];
        $endtime=$_POST['endtime'];
        $price=$_POST['sum_price'];//总价
        $room_id=$_POST['room_id'];
        $user_name=$_POST['user_name'];
        $user_IDcard=$_POST['user_IDcard'];
        $data['room_id']=$room_id;
        $data['starttime']=$starttime;
        $data['endtime']=$endtime;
        $data['category']=$category;
        $data['user_name']=$user_name;
        $data['user_idcard']=$user_IDcard;
        $data['price']=$price;
        $data['order_status']='已支付';
        $data['instatus']='已入住';
        $data['order_time']=date("Y-m-d H-m-s");
        $data['operator']=$_SESSION['employe_id'];
        $data['order_time']=date("Y-m-d H-m-s");
        $record=M('user')->where("user_idcard='$user_IDcard'")->count();


        $record_StandardSingle1=M('room_order')->where("user_idcard='$user_IDcard' and starttime='$starttime' and endtime='$endtime' and order_status='已支付' and category='标准单人房'")->count();
        $record_StandardSingle2=M('room_order')->where("user_idcard='$user_IDcard' and starttime='$starttime' and endtime='$endtime' and order_status='到店付' and category='标准单人房'")->count();
        $record_StandardDouble1=M('room_order')->where("user_idcard='$user_IDcard' and starttime='$starttime' and endtime='$endtime' and order_status='已支付' and category='标准双人房' ")->count();
        $record_StandardDouble2=M('room_order')->where("user_idcard='$user_IDcard' and starttime='$starttime' and endtime='$endtime' and order_status='到店付' and category='标准双人房'")->count();
        $record_LuxuriousSingle1=M('room_order')->where("user_idcard='$user_IDcard' and starttime='$starttime' and endtime='$endtime' and order_status='已支付'and category='豪华单人房'")->count();
        $record_LuxuriousSingle2=M('room_order')->where("user_idcard='$user_IDcard' and starttime='$starttime' and endtime='$endtime' and order_status='到店付' and category='豪华单人房'")->count();
        $record_LuxuriousDouble1=M('room_order')->where("user_idcard='$user_IDcard' and starttime='$starttime' and endtime='$endtime' and order_status='已支付' and category='豪华双人房'")->count();
        $record_LuxuriousDouble2=M('room_order')->where("user_idcard='$user_IDcard' and starttime='$starttime' and endtime='$endtime' and order_status='到店付' and category='豪华双人房'")->count();

        $check=M('room_order')->where("room_id='$room_id'and starttime='$starttime' and endtime='$endtime' and order_status='已支付'")->count();

        //判断该用户是否有同个时间段的订单
        if(empty($record_StandardSingle1)&&empty($record_StandardSingle2)&&empty($record_StandardDouble1)&&empty($record_StandardDouble2)&&empty($record_LuxuriousSingle1)&&empty($record_LuxuriousSingle2)&&empty($record_LuxuriousDouble1)&&empty($record_LuxuriousDouble2)){
            if(empty($check)){
                if(empty($record)){//判断是否本店注册会员
                    $data['user_id']='88888';
                    $flag = M('room_order')->add($data);
                    if($flag!==false){
                        $r_order_id=M('room_order')->where("room_id='$room_id' and starttime='$starttime' and endtime='$endtime' and instatus='已入住'")->field('r_order_id')->select();

                        $data['time']=date("Y-m-d H-m-s");
                        $data['operation']="办理到店客户入住";
                        $data['operator']=$_SESSION['employe_id'];
                        $data['order_id']=$r_order_id[0]{'r_order_id'};
                        $data['room_id']=$room_id;
                        M('log')->add($data);
                        $this->success("办理成功，入住 $room_id ,欢迎入住！",U("roomorder/storepay"),6);
                    }else{
                        $this->error("系统异常，请重试！");
                    }
                }else{//是注册会员
                    $user_ids=M('user')->where("user_idcard=$user_IDcard")->field("user_id")->select();
                    $user_id=$user_ids[0]{'user_id'};
                    $user_mobiles=M('user')->where("user_idcard=$user_IDcard")->field("user_mobile")->select();
                    $data['user_mobile']=$user_mobiles[0]{'user_mobile'};
                    $data['user_id']=$user_id;
                    $flag = M('room_order')->add($data);
                    if($flag!==false){
                        $r_order_id=M('room_order')->where("room_id='$room_id' and starttime='$starttime' and endtime='$endtime' and instatus='已入住'")->field('r_order_id')->select();
                        $data['time']=date("Y-m-d H-m-s");
                        $data['operation']="办理到店客户入住";
                        $data['operator']=$_SESSION['employe_id'];
                        $data['order_id']=$r_order_id[0]{'r_order_id'};
                        $data['room_id']=$room_id;
                        M('log')->add($data);
                        $this->success("办理成功，入住 $room_id ,欢迎入住！",U("roomorder/storepay"),6);
                    }else{
                        $this->error("系统异常，请重试！");
                    }
                }
            }else{
                $this->error("请根据系统提示进行选择房号！");
            }}
        else{
            $this->error('你已经有该时间段的订单，请查看个人订单！',U("roomorder/storepay"),5);
        }
    }
    public function replace(){
        $this->checkSession();
        $this->display();
    }
    public function replace_room(){//改签
        $this->checkSession();
        $yesterday=date("Y-m-d",strtotime("-1 day"));
        $room_id=$_POST['room_id'];
        $user_name=$_POST['user_name'];
        $now=date("Y-m-d");
        $record=M('room_order')->where("room_id='$room_id'and user_name='$user_name'and starttime>='$yesterday'and order_status='已支付'")->count();

        if(!empty($record)){
            $sql = "select * from hotel_room_order where user_name='$user_name' AND starttime>='$yesterday'and room_id='$room_id' and  order_status='已支付'";//显示订单入住时间在今天
            $query = M('room_order')->query($sql);
            $this->assign('param', $query);
            $this->display();
        }else{
            $this->error("信息不匹配，请重试！");
        }
    }
    public function edit_replace(){
        $this->checkSession();
        $r_order_id=$_GET['r_order_id'];
        $info = M('room_order')->where("r_order_id=$r_order_id")->find();
        $this->assign('info',$info);





        $starttime = $info['starttime'];//入住时间
        $endtime =$info['endtime'];//退房时间
        $a =strtotime($starttime);
        $b=strtotime($endtime);
        $type = $info['category'];
        $roomId_count = M('room')->where("room_category='$type'")->field('room_id')->count();//数组长度
        $roomId = M('room')->where("room_category='$type'")->field('room_id')->select();//数组内容
        $sum = 0;
        $n=0;
        for ($i = 0; $i <= $roomId_count; $i++) {//循环输出数组内容

            $rid = $roomId[$i]['room_id'];

            $room_order = M('room_order');
            $a = $room_order->where("room_id='$rid'and starttime<='$starttime'and endtime>'$starttime' and starttime<'$endtime' and endtime>='$endtime'and order_status='到店付'")->select();//内包含
            $a1 = $room_order->where("room_id='$rid'and starttime<='$starttime'and endtime>'$starttime' and starttime<'$endtime' and endtime>='$endtime'and order_status='已支付'")->select();//内包含

            $b = $room_order->where("room_id='$rid'and starttime<='$starttime'and endtime>'$starttime' and  endtime<='$endtime'and order_status='到店付'")->select();//右半包含
            $b1 = $room_order->where("room_id='$rid'and starttime<='$starttime'and endtime>'$starttime' and  endtime<='$endtime'and order_status='已支付'")->select();//右半包含

            $c = $room_order->where("room_id='$rid'and starttime>='$starttime'and  starttime<'$endtime' and endtime>='$endtime'and order_status='到店付'")->select();//左半包含
            $c1 = $room_order->where("room_id='$rid'and starttime>='$starttime'and  starttime<'$endtime' and endtime>='$endtime'and order_status='已支付'")->select();//左半包含

            $d = $room_order->where("room_id='$rid'and starttime>='$starttime' and endtime<='$endtime'and order_status='到店付'")->select();//全包含
            $d1 = $room_order->where("room_id='$rid'and starttime>='$starttime' and endtime<='$endtime'and order_status='已支付'")->select();//全包含

            $t = $roomId_count;//该类型的房间数量

            if ($rid != $roomId[$t]['room_id']) {
                if (empty($a) && empty($a1) && empty($b) && empty($b1) && empty($c) && empty($c1) && empty($d) && empty($d1)) {

                    $sum = $sum+1;
                    $price=M('room')->where("room_id='$rid'")->field('room_price')->select();
                    $room_price=$price[0]{'room_price'};
                    $id[$n]['room_id']=$rid;
                    // $this->assign("room_id",$id[$n]['room_id']);
                    $n+=1;
                } else {
                    continue;
                }
            } else {
                break;
            }
        }
        if($sum==0){
            $this->error('抱歉，该时间段该房型已经满房！',U('roomorder/storepay'),5);
        }

        $this->assign('list',$id);

        $this->display();
    }
    public function edit_replaces(){
        $data['room_id']=$_POST['room_id'];
        $new_room_id=$_POST['room_id'];
        $old_room_id=$_POST['old_room_id'];
        $data['operator']=$_SESSION['employe_id'];
        $r_order_id=$_POST['r_order_id'];
        $result=M('room_order')->where("r_order_id='$r_order_id'")->save($data);
        if($result!==false){
            $data['time']=date("Y-m-d H-m-s");
            $data['operation']="办理改签($old_room_id 房改为 $new_room_id 房)";
            $data['operator']=$_SESSION['employe_id'];
            $data['order_id']=$r_order_id;
            $data['room_id']=$_POST['room_id'];
            M('log')->add($data);
            $this->success('改签成功！',U("roomorder/replace"));
        }else{
            $this->error("改签失败！");
        }
    }
    public function checkSession(){
        $employe_id = $_SESSION['employe_id'];
        if(isset($employe_id)){
            $this->assign('employe_id',$employe_id);
        }else{
            $this->error("需先登录,方可进行查询！",U('Login/index'));
        }
    }
}